// EditScriptView.cpp : implementation file
//

#include "stdafx.h"
#include "waveeditor.h"
#include "waveeditordoc.h"
#include "EditScriptView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEditScriptView

IMPLEMENT_DYNCREATE(EditScriptView, CEditView)

EditScriptView::EditScriptView():myedit(GetEditCtrl())
{
}

EditScriptView::~EditScriptView()
{
}


BEGIN_MESSAGE_MAP(EditScriptView, CEditView)
	//{{AFX_MSG_MAP(CEditScriptView)
	ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
	ON_COMMAND(ID_EDITSCRIPT_RUN, OnEditscriptRun)
	ON_COMMAND(ID_EDITSCRIPT_SAVE, OnEditscriptSave)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditScriptView drawing

void EditScriptView::OnDraw(CDC* pDC)
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CEditScriptView diagnostics

#ifdef _DEBUG
void EditScriptView::AssertValid() const
{
	CEditView::AssertValid();
}

void EditScriptView::Dump(CDumpContext& dc) const
{
	CEditView::Dump(dc);
}

ZWaveeditorDoc* EditScriptView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ZWaveeditorDoc)));
	return (ZWaveeditorDoc*)m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEditScriptView message handlers

BOOL EditScriptView::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style	|=	
					ES_MULTILINE   
					|ES_AUTOVSCROLL
					|ES_AUTOHSCROLL 
					|ES_LEFT 
					|ES_OEMCONVERT   
					|ES_WANTRETURN
					|ES_NOHIDESEL  
					|WS_HSCROLL
					|WS_VSCROLL;

	return CEditView::PreCreateWindow(cs);
}

void EditScriptView::OnInitialUpdate() 
{
	CEditView::OnInitialUpdate();
	SelectedScenario = GetDocument()->selectedscenario;
	UpDateData();
}

void EditScriptView::ChangeWindowTitle()
{
	CFrameWnd* pFrame	=	GetParentFrame();
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CString	name	=	"Edit Script (";
	name			+=	pDoc->GetTitle()+ ")";
	pFrame->SetWindowText(name);
	if(pDoc->CheckIfEditWindowOpened() == FALSE)
	{
		UpDateData();
	}

}

void EditScriptView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	ChangeWindowTitle();	
}

void EditScriptView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
	ChangeWindowTitle();	
	CEditView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}


void EditScriptView::OnKillfocus() 
{
	// TODO: Add your control notification handler code here
	ChangeWindowTitle();	
	
}

void EditScriptView::OnEditscriptRun() 
{
	// TODO: Add your command handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->OnEditscriptRun(&myedit, SelectedScenario);
}

void EditScriptView::OnEditscriptSave() 
{
	// TODO: Add your command handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	myedit.UpdateData(FALSE);
	pDoc->OnEditscriptSave(&myedit, SelectedScenario);
	pDoc->SetModifiedFlag(FALSE);
}


void EditScriptView::OnDestroy() 
{
	
	// TODO: Add your message handler code here
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->IsModified() == TRUE)
	{
		int res	=	MessageBox("Do you want to save changes","Warning Message",MB_YESNO);
		if(res	==	IDYES)
		{
			pDoc->OnEditscriptSave(&myedit, SelectedScenario);
		}
	}
	pDoc->SetEditWindowClosed();
	CEditView::OnDestroy();
	
}

int EditScriptView::ReadScriptFromSource()
{
	ZWaveeditorDoc* pDoc = GetDocument();

	int numoflines	=	pDoc->templist.GetNumberOfMessages();
	CString			result="";
	for(int i=0;i<numoflines;i++)
	{
		result	+=	pDoc->templist.GetMessage(i);
	}
	myedit.SetWindowText(result);
	myedit.FmtLines(TRUE);
	return 0;
}

void EditScriptView::UpDateData()
{
	ZWaveeditorDoc* pDoc = GetDocument();
	pDoc->SetModifiedFlag(FALSE);
	ReadScriptFromSource();
}
